package com.htgd.gardenexpo.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.dto.GeConDeviceMealDTO;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConIntegralGoods;
import com.htgd.gardenexpo.page.GoodsQuery;
import com.htgd.gardenexpo.page.PageBase;
import com.htgd.gardenexpo.page.RecommendQuery;
import com.htgd.gardenexpo.service.DeviceService;
import com.htgd.gardenexpo.vo.GeConDeviceMealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: 王秀田
 * @date: 2024/11/22 13:54
 * @description: 设备管理套餐管理
 */
@RequestMapping("tiaoz")
@Api(tags = "设备管理套餐管理")
@RestController
@Slf4j
public class DeviceMealController {

    @Autowired
    private DeviceService deviceService;

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult<JSONObject> add(@RequestBody GeConDeviceMealDTO geConDeviceMeal) {
        log.info("设备套餐-添加：{}",geConDeviceMeal);
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            deviceService.add(geConDeviceMeal);
            result.setCode(200);
            result.setMessage("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    @Transactional
    public CommonResult<JSONObject> edit(@RequestBody GeConDeviceMealDTO geConDeviceMeal) {
        log.info("设备套餐-编辑：{}",geConDeviceMeal);
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            deviceService.update(geConDeviceMeal);
            result.setCode(200);
            result.setMessage("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    @Transactional
    public CommonResult<JSONObject> delete(@RequestParam(value = "id") String id,@RequestParam(value = "type") String type) {
        log.info("设备套餐-删除：{},{}",id,type);
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            deviceService.delete(id,type);
            result.setCode(200);
            result.setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeConDeviceMealVO>> list(RecommendQuery pageBase) {
        log.info("设备套餐-列表查询：{}",pageBase);
        CommonResult<List<GeConDeviceMealVO>> result = new CommonResult<>();
        try {
            PageInfo<GeConDeviceMealVO> list = deviceService.pageList(pageBase);
            result.setCode(200);
            result.setMessage(String.valueOf(list.getTotal()));
            result.setData(list.getList());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

}
